package com.zingrow.web.configure.distribute.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import com.google.common.base.Strings;
import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.common.utils.ExcelUtils;
import com.zingrow.common.utils.OptClass;
import com.zingrow.common.utils.PageView;
import com.zingrow.common.utils.StringUtil;
import com.zingrow.common.utils.WordGenerator;
import com.zingrow.web.base.CodeMsg;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.base.enums.Menu;
import com.zingrow.web.base.enums.Operation;
import com.zingrow.web.configure.distribute.model.InformationDistributeModel;
import com.zingrow.web.configure.distribute.service.IInformationDistributeService;
import com.zingrow.web.configure.iformation.service.IIformationService;
import com.zingrow.web.customer.model.Group;
import com.zingrow.web.customer.service.IGroupSchemeService;
import com.zingrow.web.customer.service.IGroupService;
import com.zingrow.web.support.operate.model.OptRecord;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.user.model.ActiveUser;
import com.zingrow.web.user.model.UserPermission;
import com.zingrow.web.user.service.IPermissionService;
@Controller
@RequestMapping(value="/Distribute/",produces="application/json;charset=UTF-8")
public class IformationDistributeController extends BaseController {
	@Autowired
	private IInformationDistributeService iInformationDistributeService;
	@Autowired
	private IGroupService groupService;
	@Autowired
	private IOptRecordService optRecordService;
	@Autowired
	private IGroupSchemeService groupSchemeService;
	@Autowired
	private IPermissionService permissionService;
	@Autowired
	private IOptRecordService ioptRecordService;
	@Autowired
	private IIformationService iformationService;


	
	/**
	 * // 单个查询
	 * @param request
	 * @param response
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("find")
	public String find(HttpServletRequest request,
			HttpServletResponse response, String articleId,String ifmId) {
		InformationDistributeModel buteModel = new InformationDistributeModel();
		ReturnStandardDataFormat dataFormat = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			// 判断参数是否正确
			if (CommonUtils.isObjectNull(articleId)) {
				dataFormat.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				dataFormat.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
			}
			// 调用查询方法得到list集合
			buteModel = iInformationDistributeService.selectByid(
						InformationDistributeModel.class, articleId,ifmId);
				// 判断集合是否为空
				if (CommonUtils.isObjectNotNull(buteModel)) {
					// 判断类型
						if (buteModel.getArticleType().equals("Micro")) {
						buteModel.setContent(buteModel.getWeiboContent()
								+ buteModel.getWeiboSourceContent());
						buteModel.setSource(buteModel.getWeiboBlogger());
						//添加微博查看操作日志
						//新增一条操作记录
						//操作日志
						ActiveUser opuser = getUser(request);//获取操作用户
						OptRecord s = new OptClass().dist_Update(opuser,Menu.informationmanagement.toString(),
								Menu.distribute.toString(),Operation.querys.toString(),buteModel.getUrl(),1);
						ioptRecordService.insert(s);	
					}
				}
			} catch (Exception e) {
			log.error("根据编号查询异常");
			log.error(e.getMessage());
			dataFormat.setReturnCode(CodeMsg.CODE_EXCEPTION);
			dataFormat.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			dataFormat.setReturnData(buteModel);
		}
		dataFormat.setReturnData(buteModel);
		dataFormat.setReturnCode(CodeMsg.CODE_OK);
		dataFormat.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
	}

	
	/**
	 * // 编辑单个查询查询
	 * @param InformationDistributeModel
	 * @param articleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("findid")
	public String findid(Class<?> InformationDistributeModel, String articleId,String ifmId,Integer groupIdS,
			Integer ifmIdS,String Id) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		List<InformationDistributeModel> listbute = new ArrayList<InformationDistributeModel>();
		ReturnStandardDataFormat dataFormat = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			/*// 判断参数是否正确
			if (CommonUtils.isObjectNull(articleId)) {
				dataFormat.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				dataFormat.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
			}*/
			if (ifmIdS != null && groupIdS != null && Id != null) {
				InformationDistributeModel model = iInformationDistributeService
						.findId(InformationDistributeModel.class, Id);
				Group group = groupService.selectgroupName(groupIdS);
				if(CommonUtils.isObjectNotNull(group)){
					model.setGroupName(group.getGroupName());
					model.setTissueId(groupIdS.toString());
				}
				list.add(model);
			}else if(groupIdS != null && articleId !=null){
				// 调用查询方法得到list集合
				list = iInformationDistributeService.findifmId(
						InformationDistributeModel.class, articleId, null,groupIdS);
				if(list.size()>0){
					for (InformationDistributeModel buteModel2 : list) {
						//根据组织ID查询组织名称
						Group group = groupService.selectgroupName(groupIdS);
						if(CommonUtils.isObjectNotNull(group)){
							buteModel2.setGroupName(group.getGroupName());
							buteModel2.setTissueId(groupIdS.toString());
						}
						listbute.add(buteModel2);
					}
				}
			} else {
				// 调用查询方法得到list集合
				list = iInformationDistributeService.findifmId(
						InformationDistributeModel.class, articleId, ifmId,groupIdS);
				// 判断list集合是否为空
				if (list.size() > 0) {
					for (InformationDistributeModel bute : list) {
						List<String> groupName = new ArrayList<String>();
						List<String> groupId = new ArrayList<String>();
						// 根据集合对象中的方案id查询组织
						List<Group> listgroup = groupService.selectfidName(bute
								.getIfmId());
						String Name = null;
						String gId = null;
						if (listgroup.size() > 0) {
							if (listgroup.size() != 0) {
								for (Group group : listgroup) {
									// 组织名称
									String str = group.getGroupName();
									groupName.add(str);
									Name = StringUtil.toStringjoin(groupName);
									// 组织id
									Integer id = group.getGroupId();
									groupId.add(id.toString());
									gId = StringUtil.toStringjoin(groupId);
									bute.setTissueId(gId);
									bute.setGroupName(Name);
								}
								listbute.add(bute);
							}
						}
					}
				}
			}
		}catch (Exception e) {
			log.error("查询异常");
			log.error(e.toString());
			dataFormat.setReturnCode(CodeMsg.CODE_EXCEPTION);
			dataFormat.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			//return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
		}
		dataFormat.setReturnData(listbute.size()>0?listbute:list);
		dataFormat.setReturnCode(CodeMsg.CODE_OK);
		dataFormat.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
	}
		//查询全部
		/*@ResponseBody
		@RequestMapping("findAll")
		public String findAl(HttpServletRequest request,HttpServletResponse response,String releaseTime1,String releaseTime2,
				Integer pageIndex,Integer pageSize){
			List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
			PageView pageView = null;
			long count = 0;
			long test = 0;
			ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,list);
			try {
				if(pageIndex == null || pageIndex <= 0){
					pageIndex = 1;
				}
				if(pageSize == null || pageSize <= 0){
					pageSize = 10;
				}
				pageView = new PageView(pageSize,pageIndex);
				Date startTime = null;
				Date endTime = null;
				if(releaseTime1 !=null && releaseTime1 !="" && releaseTime2 != null && releaseTime2 !=""){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
					startTime = sdf.parse(releaseTime1);
					endTime = sdf.parse(releaseTime2);
				}
				list = iInformationDistributeService.findAl(InformationDistributeModel.class, startTime, endTime,pageView);
				if(releaseTime1 !=null && releaseTime1 !="" && releaseTime2 != null && releaseTime2 !=""){
					count = list.get(0).getCount();
				}else{
					count = iInformationDistributeService.selectcount(InformationDistributeModel.class);
				}
				//统计数据
				
				//计算有多少页
				test = count%pageView.getPageSize()==0 ? count/pageView.getPageSize() : count/pageView.getPageSize()+1;
				standardData.setTotalPages(test);
				standardData.setTotalRecords(count);
				for (InformationDistributeModel model : list) {
					if(model.getArticleType().equals("Micro")){
						model.setContent(model.getWeiboSourceContent()+model.getWeiboContent());
						model.setWebsiteName(model.getWeiboBlogger());
					}
					if(model.getContent()!=null){
						String content = model.getContent().replaceAll("</?[^>]+>", "");				
						model.setContent(content);
					}
				}
				} catch (Exception e) {
					log.error("查询异常");
					log.error(e.getMessage());
					standardData.setReturnData(list);
					standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
					standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				}
					standardData.setReturnData(list);
					standardData.setReturnCode(CodeMsg.CODE_OK);
					standardData.setReturnMessage(CodeMsg.MSG_OK);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}*/
	
	/**
	 * // 多条件模糊分页查询
	 * @param request
	 * @param response
	 * @param title
	 * @param content
	 * @param websiteName
	 * @param releaseTime1
	 * @param releaseTime2
	 * @param articleType
	 * @param pageIndex
	 * @param pageSize
	 * @param groupName
	 * @param ifmId
	 * @param groupId
	 * @param url
	 * @return
	 */
	@ResponseBody
	@RequestMapping("findGroup")
	public String findGroup(HttpServletRequest request,
			HttpServletResponse response, String title, String content,
			String websiteName,@RequestParam(required = false) String releaseTime1,
			@RequestParam(required = false) String releaseTime2,String articleType,
			Integer pageIndex,Integer pageSize,String groupName,String ifmIdd,String groupId, String url,String sp) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 总条数
		long count = 0;
		// 总页数
		long number = 0;
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, list);
		PageView pageView = null;
		try {
			// 初始值
			if (pageIndex == null || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (pageSize == null || pageSize <= 0) {
				pageSize = 10;
			}
			pageView = new PageView(pageSize, pageIndex);
			// 字符转换
			if (title != "" || "".equals(title)) {
				title = EncodingUtils.fixUrlParamEncoding(request, title);
			}
			if (content != "" || "".equals(content)) {
				content = EncodingUtils.fixUrlParamEncoding(request, content);
			}
			if (websiteName != "" || "".equals(websiteName)) {
				websiteName = EncodingUtils.fixUrlParamEncoding(request,
						websiteName);
			}
			if (articleType != "" || "".equals(articleType)) {
				articleType = EncodingUtils.fixUrlParamEncoding(request,
						articleType);
			}
			if (groupName != "" || "".equals(groupName)) {
				groupName = EncodingUtils.fixUrlParamEncoding(request,
						groupName);
			}
			String content1=content;
			//解决高亮和未高亮查询问题
			if (StringUtils.isNoneBlank(content)) {
				content = StringUtil.getLikeAppString(content);
			}
			// String类型转换为Date类型
			Date startTime = null;
			Date endTime = null;
			if (StringUtils.isNotBlank(releaseTime1) && StringUtils.isNotBlank(releaseTime2)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				startTime = sdf.parse(releaseTime1);
				endTime = sdf.parse(releaseTime2);
			}
			List<String> idlist = new ArrayList<String>();
			// 根据组织查询组织下所有方案中所有数据
			if (StringUtils.isNoneBlank(groupId) && StringUtils.isBlank(ifmIdd)) {
				List<Group> listscheme = groupService.selectgroupid(Integer.parseInt(groupId));
				if(listscheme.size()!=0){
					for (Group group : listscheme) {
						idlist.add(group.getIfmIdd());
					}
				}else{
					standardData.setReturnData(listscheme);
					// 总页数set到standardData中
					standardData.setTotalPages(number);
					// 总条数set到standardData中
					standardData.setTotalRecords(count);
					standardData.setReturnCode(CodeMsg.CODE_OK);
					standardData.setReturnMessage(CodeMsg.MSG_OK);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				}
			} 
			if (StringUtils.isNoneBlank(ifmIdd)) {
				// 根据组织和方案查询数据
				idlist.add(ifmIdd);
			}
			if (StringUtils.isNotBlank(url)) {
				url = url.replace("?", "\\?").replaceAll("&", "\\&").replaceAll("_", "\\_").replace("%", "\\%");
			}
			if (StringUtils.isNotBlank(title)) {
				title = title.replace("+", "\\+").replaceAll("“", "\\“").replaceAll("”", "\\”");
			}
			if (StringUtils.isNotBlank(content)) {
				content = content.replace("+", "\\+").replaceAll("“", "\\“").replaceAll("”", "\\”");
			}
			// 调用查询方法得到list集合
			list = iInformationDistributeService.findGroup(
					InformationDistributeModel.class, title, content,
					websiteName, startTime, endTime, articleType, pageView,
					idlist, url);
			// 判断list集合是否为空
			if (list.size() > 0) {
				// 得到总条数
				count = list.get(0).getCount();
				// 计算有多少页
				number = count % pageView.getPageSize() == 0 ? count
						/ pageView.getPageSize() : count
						/ pageView.getPageSize() + 1;
			}
			//新增一条操作记录
			//操作日志
			if("11".equals(sp))
			{//判断是否为自主点击查询按钮*/
	        ActiveUser opuser = getUser(request);//获取操作用户
			String ifms=null,groups=null;
			List<String> lists=new ArrayList<String>();
			if(groupId!=null&&!"".equals(groupId))
			groups=groupService.selectGroupNameByGroupid(Integer.parseInt(groupId));
			if(ifmIdd!=null&&!"".equals(ifmIdd)){   
				lists.add(ifmIdd);
				ifms=iformationService.selectlistId(lists).get(0).getIfm_name();}
			OptRecord s = new OptClass().dist_query(opuser,Menu.informationmanagement.toString(),
					Menu.distribute.toString(),Operation.query.toString(),title,content1,websiteName,articleType,url,ifms,groups,releaseTime1,releaseTime2);
			if(s.getOptName()!="null")
			ioptRecordService.insert(s);
			}
		} catch (Exception e) {
			log.error("查询发生异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"已发布信息查询异常");
			ioptRecordService.insert(s);
			standardData.setReturnData(list);
			// 总页数set到standardData中
			standardData.setTotalPages(number);
			// 总条数set到standardData中
			standardData.setTotalRecords(count);
			standardData.setReturnCode(CodeMsg.CODE_ERROR);
			standardData.setReturnMessage(CodeMsg.MSG_ERROR);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}				
		standardData.setReturnData(list);
		// 总页数set到standardData中
		standardData.setTotalPages(number);
		// 总条数set到standardData中
		standardData.setTotalRecords(count);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	
	/**
	 * // 修改
	 * @param request
	 * @param response
	 * @param bute
	 * @return
	 */
	@ResponseBody
	@RequestMapping("Update")
	public String Update(HttpServletRequest request,
			HttpServletResponse response, InformationDistributeModel bute,String title) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			if(title!=null || "".equals(title)) {
				title=EncodingUtils.fixUrlParamEncoding(request, title);
			}
			// 判断参数是否正确
			if (CommonUtils.isStrNull(bute.getTitle())
					|| CommonUtils.isStrNull(bute.getUrl())
					|| CommonUtils.isStrNull(bute.getContent())) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			} else {
				// 中文转码
				bute.setTitle(EncodingUtils.fixUrlParamEncoding(request,
						bute.getTitle()));
				bute.setWebsiteName(EncodingUtils.fixUrlParamEncoding(request,
						bute.getWebsiteName()));
				bute.setAuthor(EncodingUtils.fixUrlParamEncoding(request,
						bute.getAuthor()));
				bute.setUrl(EncodingUtils.fixUrlParamEncoding(request,
						bute.getUrl()));
				bute.setContent(EncodingUtils.fixUrlParamEncoding(request,
						bute.getContent()));
				bute.setArticleType(EncodingUtils.fixUrlParamEncoding(request,
						bute.getArticleType()));
			}
			// 调用修改
			if (iInformationDistributeService.update(bute)) {
				// 新增一条操作记录
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().dist_Update(opuser,Menu.informationmanagement.toString(),
						Menu.distribute.toString(),Operation.update.toString(),title,0);
				if(s.getOptName()!="null")
				ioptRecordService.insert(s);
				// 修改成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				// 修改失败
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
		} catch (Exception e) {
			log.error("修改异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"已发布修改异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	
	/**
	 * 删除方案与组织关系
	 * @param request
	 * @param response
	 * @param articleId
	 * @param ifmId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, String articleId, Integer ifmId,String title) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			// 判断参数的合法性
			InformationDistributeModel buteModel = new InformationDistributeModel();
			buteModel = iInformationDistributeService.selectByid(
					InformationDistributeModel.class,articleId,ifmId.toString());	
			if (title != "" || "".equals(title)) {
				title = EncodingUtils.fixUrlParamEncoding(request, title);
			}
			if (CommonUtils.isObjectNull(ifmId)) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}			
				if(iInformationDistributeService.delete(articleId, ifmId.toString())){
					ActiveUser opuser = getUser(request);//获取操作用户
					if(title!=null&&!"".equals(title)&&!"null".equals(title))
					{
					OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
							Menu.distribute.toString(),Operation.delete.toString(),title,"1");
					if(s.getOptName()!="null")
					ioptRecordService.insert(s);
					}
					else 
					{
					OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
							Menu.distribute.toString(),Operation.delete.toString(),buteModel.getUrl(),"2");
					if(s.getOptName()!="null")
					ioptRecordService.insert(s);
					}
					standardData.setReturnCode(CodeMsg.CODE_OK);
					standardData.setReturnMessage(CodeMsg.MSG_OK);
				}else{
					// 操作失败
					standardData.setReturnCode(CodeMsg.CODE_ERROR);
					standardData.setReturnMessage(CodeMsg.MSG_ERROR);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				}
		} catch (Exception e) {
			log.error("删除异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	
	/**
	 * // 删除
	 * @param request
	 * @param response
	 * @param groupId
	 * @param groupvale
	 * @return
	 */
	/*@ResponseBody
	@RequestMapping("deleteid")
	public String deleteid(HttpServletRequest request,
			HttpServletResponse response, String groupId,@RequestParam(required = false) 
			String releaseTime1,@RequestParam(required = false) String releaseTime2,
			@RequestParam(required = false)String ifmIdd) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			// 判断参数的合法性
			if (CommonUtils.isObjectNull(groupId)) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			// String类型转换为Date类型
			Date startTime = null;
			Date endTime = null;
			if (StringUtils.isNotBlank(releaseTime1) && StringUtils.isNotBlank(releaseTime2)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				startTime = sdf.parse(releaseTime1);
				endTime = sdf.parse(releaseTime2);
			}
			if (iInformationDistributeService.deleteByField(InformationDistributeModel.class,groupId,startTime,endTime
					,ifmIdd)) {
				// 新增一条操作记录
				addOptRecode(request, optRecordService,
						Menu.distribute.toString(),
						Operation.delete.toString(), null);
				// 操作成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				// 操作失败
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
		} catch (Exception e) {
			log.error("删除异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
*/
	
	/**
	 * // 导出列表
	 * @param request
	 * @param response
	 * @param articleId
	 * @param title
	 * @param content
	 * @param websiteName
	 * @param releaseTime1
	 * @param releaseTime2
	 * @param articleType
	 * @param groupName
	 * @param ifmId
	 * @param groupId
	 * @param url
	 * @return
	 */
	/*@ResponseBody
	@RequestMapping("exportExcelForPageData")
	public String exportExcelForPageData(HttpServletRequest request,
			HttpServletResponse response, String articleId, String title,
			String content, String websiteName,
			@RequestParam(required = false) String releaseTime1,
			@RequestParam(required = false) String releaseTime2,
			String articleType, String groupName, String ifmId, String groupId,
			String url) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			// 定义表头
			List<String> titls = Arrays.asList("所属客户", "所属方案", "信息类型", "发布时间",
					"标题", "首发媒体", "站点", "作者", "正文统计数", "登载量", "是否原创", "评级(研报)",
					"阅读量(微信,论坛)", "点赞量(微信)", "回复数(论坛)", "文章链接", "标签");
			// 定义表的字段名
			List<String> paramsName = Arrays.asList("userName", "ifmName",
					"articleType", "releaseTime", "title", "websiteName",
					"domainName","author", "content", "reprintNumb", "groupId", "",
					"readNumb", "pointNum", "replyNumb", "url", "websiteSign");
			// 处理传入的id，并查询出列表
			List<String> idlist = new ArrayList<>();
			List<InformationDistributeModel> bute = null;
			if (StringUtils.isNoneBlank(articleId)) {
				// 字符串分割
				for (String strids : articleId.split(",")) {
					idlist.add(strids);
				}
				// 根据id查询的到集合
				bute = iInformationDistributeService.findAllid(
						InformationDistributeModel.class, idlist);
			} else {
				// 字符转换
				if (title != "" || "".equals(title)) {
					title = EncodingUtils.fixUrlParamEncoding(request, title);
				}
				if (content != "" || "".equals(content)) {
					content = EncodingUtils.fixUrlParamEncoding(request,
							content);
				}
				if (websiteName != "" || "".equals(websiteName)) {
					websiteName = EncodingUtils.fixUrlParamEncoding(request,
							websiteName);
				}
				if (articleType != "" || "".equals(articleType)) {

					articleType = EncodingUtils.fixUrlParamEncoding(request,
							articleType);
				}
				if (groupName != "" || "".equals(groupName)) {
					groupName = EncodingUtils.fixUrlParamEncoding(request,
							groupName);
				}
				Date startTime = null;
				Date endTime = null;
				// 时间转换
				if (StringUtils.isNotBlank(releaseTime1) && StringUtils.isNotBlank(releaseTime2)) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					startTime = sdf.parse(releaseTime1);
					endTime = sdf.parse(releaseTime2);
				}
				List<String> ids = new ArrayList<String>();
				if (!Strings.isNullOrEmpty(groupId) && ifmId == null || "".equals(ifmId)) {
					// 根据组织id查询组织下的所有方案
					List<Group> listscheme = groupService.selectgroupid(Integer
							.parseInt(groupId));
					for (Group group : listscheme) {
						ids.add(group.getIfmIdd());
					}
				} else if (!Strings.isNullOrEmpty(groupId)
						&& !Strings.isNullOrEmpty(ifmId)) {
					ids.add(ifmId);
				}
				// 根据条件查询得到list集合
				bute = iInformationDistributeService.findGroupAll(
						InformationDistributeModel.class, title, content,
						websiteName, startTime, endTime, articleType, url, ids);
			}
			// 定义一个输出流
			OutputStream out = null;
			try {
				// 获取输出流
				out = response.getOutputStream();
				// 清空流
				response.reset();
				// 定义响应输出类型
				response.setContentType("application/msexcel");
				// 设置输出文件头
				response.setHeader(
						"Content-disposition",
						"attachment; filename="
								+ new String("已发布信息列表".getBytes("GBK"),
										"iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titls, paramsName, bute, out);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				// 关闭流
				out.close();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}*/

	
	/**
	 * // 导出word
	 * @param request
	 * @param response
	 * @param articleId
	 * @param title
	 * @param content
	 * @param websiteName
	 * @param releaseTime1
	 * @param releaseTime2
	 * @param articleType
	 * @param groupName
	 * @param ifmId
	 * @param groupId
	 * @param url
	 * @return
	 */
	@ResponseBody
	@RequestMapping("exportWordForPageData")
	public String exportWordForPageData(HttpServletRequest request,
			HttpServletResponse response, String articleId, String title,
			String content, String websiteName,
			@RequestParam(required = false) String releaseTime1,
			@RequestParam(required = false) String releaseTime2,
			String articleType, String groupName, String ifmIdd, String groupId,
			String url) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		List<InformationDistributeModel> butelist = new ArrayList<InformationDistributeModel>();
		List<String> idlist = new ArrayList<>();
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			if (StringUtils.isNoneBlank(articleId)) {
				// 分割id
				for (String strids : articleId.split(",")) {
					idlist.add(strids);
				}
				// 字符转换
				if (title != "" || "".equals(title)) {
					title = EncodingUtils.fixUrlParamEncoding(request, title);
				}
				if (content != "" || "".equals(content)) {
					content = EncodingUtils.fixUrlParamEncoding(request,
							content);
				}
				if (websiteName != "" || "".equals(websiteName)) {
					websiteName = EncodingUtils.fixUrlParamEncoding(request,
							websiteName);
				}
				if (articleType != "" || "".equals(articleType)) {
					articleType = EncodingUtils.fixUrlParamEncoding(request,
							articleType);
				}
				if (groupName != "" || "".equals(groupName)) {
					groupName = EncodingUtils.fixUrlParamEncoding(request,
							groupName);
				}
				Date startTime = null;
				Date endTime = null;
				// 时间类型转换
				if (StringUtils.isNotBlank(releaseTime1) && StringUtils.isNotBlank(releaseTime2)) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					startTime = sdf.parse(releaseTime1);
					endTime = sdf.parse(releaseTime2);
				}
				List<String> ids = new ArrayList<String>();
				if (!Strings.isNullOrEmpty(groupId) && StringUtils.isBlank(ifmIdd)) {
					// 根据组织id查询组织下的所有方案
					List<Group> listscheme = groupService.selectgroupid(Integer
							.parseInt(groupId));
					for (Group group : listscheme) {
						ids.add(group.getIfmIdd());
					}
				} 
				if (StringUtils.isNoneBlank(ifmIdd)) {
					ids.add(ifmIdd);
				}
				// 设置字符集编码
				request.setCharacterEncoding("utf-8");
				// 根据传入的id查询数据得到集合
				butelist = iInformationDistributeService.exportWordForPageData(InformationDistributeModel.class, ids, title
						, content, websiteName, startTime, endTime, articleType, url, idlist);
				/*butelist = iInformationDistributeService.exportWordForPageData(
						InformationDistributeModel.class, idlist,String articleId, String title,
						String content, String websiteName,
						@RequestParam(required = false) String releaseTime1,
						@RequestParam(required = false) String releaseTime2,
						String articleType, String groupName, String ifmIdd, String groupId,
						String url);*/
				for (InformationDistributeModel bute : butelist) {
					Map<String, Object> dataMap = new HashMap<String, Object>();
					dataMap.put("title", bute.getTitle());
					dataMap.put("author", bute.getAuthor());
					dataMap.put("websiteName", bute.getWebsiteName());
					dataMap.put("content", bute.getContent());
					list.add(dataMap);
				}
			} 
			File file = null;
			InputStream fin = null;
			OutputStream out = null;
			try {
				// 调用工具类WordGenerator的createDoc方法生成word文档
				Map<String, Object> root = new HashMap<>();
				root.put("blist", list);
				file = WordGenerator.createDoc(root, "resume");
				fin = new FileInputStream(file);
				// 设置字符集编码
				response.setCharacterEncoding("utf-8");
				response.setContentType("application/msword");
				// 设置浏览器以下载方式处理该文件默认名为信息文章.doc
				response.addHeader(
						"Content-Disposition",
						"attachment;filename="
								+ new String("已发布信息文章".getBytes("GBK"),
										"iso-8859-1") + ".doc");

				out = response.getOutputStream();
				byte[] buffer = new byte[512]; // 缓存区
				int bytesToRead = -1;
				// 通过循环将读入的word文件的内容输出到浏览器中
				while ((bytesToRead = fin.read(buffer)) != -1) {
					out.write(buffer, 0, bytesToRead);
				}
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				if (fin != null)
					fin.close();
				if (out != null)
					out.close();// 关闭流
				if (file != null)
					file.delete(); // 删除临时文件
			}
			//操作日志
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().goiss(opuser,Menu.informationmanagement.toString(),
					Menu.distribute.toString(),Operation.Exportarticles.toString(),idlist.size());
			ioptRecordService.insert(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}

		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
	
	
	@ResponseBody
	@RequestMapping("exportExcelForPageData")
	public String exportExcelForPageData(HttpServletRequest request,
			HttpServletResponse response, String articleId, String title,
			String content, String websiteName,
			@RequestParam(required = false) String releaseTime1,
			@RequestParam(required = false) String releaseTime2,
			String articleType, String groupName, String ifmIdd, String groupId,
			String url) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(
				CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR, null);
		try {
			// 定义表头
			List<String> titls = Arrays.asList("所属客户", "所属方案", "信息类型", "发布时间",
					"标题", "首发媒体", "站点", "作者", "正文统计数", "登载量", "是否原创", "评级(研报)",
					"阅读量(微信,论坛)", "点赞量(微信)", "回复数(论坛)", "文章链接", "标签");
			// 定义表的字段名
			List<String> paramsName = Arrays.asList("userName", "ifmName",
					"articleType", "releaseTime", "title", "websiteName",
					"domainName","author", "content", "reprintNumb", "groupId", "",
					"readNumb", "pointNum", "replyNumb", "url", "websiteSign");
			// 处理传入的id，并查询出列表
			List<String> idlist = new ArrayList<>();
			List<InformationDistributeModel> bute = null;
			if (StringUtils.isNoneBlank(articleId)) {
				// 字符串分割
				for (String strids : articleId.split(",")) {
					idlist.add(strids);
				}
				// 根据id查询的到集合
			/*	bute = iInformationDistributeService.findAllid(
						InformationDistributeModel.class, idlist);*/
				// 字符转换
				if (title != "" || "".equals(title)) {
					title = EncodingUtils.fixUrlParamEncoding(request, title);
				}
				if (content != "" || "".equals(content)) {
					content = EncodingUtils.fixUrlParamEncoding(request,
							content);
				}
				if (websiteName != "" || "".equals(websiteName)) {
					websiteName = EncodingUtils.fixUrlParamEncoding(request,
							websiteName);
				}
				if (articleType != "" || "".equals(articleType)) {

					articleType = EncodingUtils.fixUrlParamEncoding(request,
							articleType);
				}
				if (groupName != "" || "".equals(groupName)) {
					groupName = EncodingUtils.fixUrlParamEncoding(request,
							groupName);
				}
				Date startTime = null;
				Date endTime = null;
				// 时间转换
				if (StringUtils.isNotBlank(releaseTime1) && StringUtils.isNotBlank(releaseTime2)) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					startTime = sdf.parse(releaseTime1);
					endTime = sdf.parse(releaseTime2);
				}
				List<String> ids = new ArrayList<String>();
				if (!Strings.isNullOrEmpty(groupId) && StringUtils.isBlank(ifmIdd)) {
					// 根据组织id查询组织下的所有方案
					List<Group> listscheme = groupService.selectgroupid(Integer
							.parseInt(groupId));
					for (Group group : listscheme) {
						ids.add(group.getIfmIdd());
					}
				}
				if (StringUtils.isNoneBlank(ifmIdd)) {
					ids.add(ifmIdd);
				}
				// 根据条件查询得到list集合
				/*bute = iInformationDistributeService.findAllidTest(
						InformationDistributeModel.class, title, content,
						websiteName, startTime, endTime, articleType, url, ids,idlist);*/
				bute = iInformationDistributeService.findAllid(InformationDistributeModel.class, 
						ids, title, content, websiteName, startTime, endTime, articleType, url, idlist);
			}
			// 定义一个输出流
			OutputStream out = null;
			try {
				// 获取输出流
				out = response.getOutputStream();
				// 清空流
				response.reset();
				// 定义响应输出类型
				response.setContentType("application/msexcel");
				// 设置输出文件头
				response.setHeader(
						"Content-disposition",
						"attachment; filename="
								+ new String("已发布信息列表".getBytes("GBK"),
										"iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titls, paramsName, bute, out);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				// 关闭流
				out.close();
			}
			//操作日志
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().goiss(opuser,Menu.informationmanagement.toString(),
					Menu.distribute.toString(),Operation.Theexportlist.toString(),idlist.size());
			ioptRecordService.insert(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
}
